using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class MaterialDetailDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procMaterialDetail_add]";
        private string _strSPUpdateName = "dbo.[procMaterialDetail_update]";
        private string _strSPDeleteName = "dbo.[procMaterialDetail_delete]";
        private string _strSPGetName = "dbo.[procMaterialDetail_get]";
        private string _strSPGetAllName = "dbo.[procMaterialDetail_getall]";
        private string _strSPGetPages = "dbo.[procMaterialDetail_getpaged]";
        private string _strSPIsExist = "dbo.[procMaterialDetail_isexist]";
        private string _strTableName = "[MaterialDetail]";
        private string _strSPGetTransferOutName = "dbo.[procMaterialDetail_gettransferout]";
        string _strSPGetCountName = "procMaterialDetail_getcount";
        string _strSPGetByIndexName = "procMaterialDetail_getindex";
        #endregion Local Variable

        #region Method
        public MaterialDetailInfo Get(
        String DTB,
        String RequestNo,
        Int32 DetailNo,
        ref string sErr)
        {
            MaterialDetailInfo objEntr = new MaterialDetailInfo();
            InitConnect();
            InitCommand(@"SELECT     DTB, RequestNo, DetailNo, ItemCode, Description, ModelPN, Unit, Qty, BudgetCode, GLAccount, Remark
                FROM         MaterialDetail
                WHERE     (DTB = @DTB) AND (RequestNo = @RequestNo) AND (DetailNo = @DetailNo)", CommandType.Text);
            AddParameter(MaterialDetailInfo.Field.DTB.ToString(), DTB);
            AddParameter(MaterialDetailInfo.Field.RequestNo.ToString(), RequestNo);
            AddParameter(MaterialDetailInfo.Field.DetailNo.ToString(), DetailNo);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }

            if (list.Rows.Count > 0)
                objEntr = new MaterialDetailInfo(list.Rows[0]);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        public DataTable GetAll(
        String DTB, string reqNo,
        ref string sErr)
        {
            InitConnect();
            InitCommand(@"SELECT     DTB, RequestNo, DetailNo, ItemCode, Description, ModelPN, Unit, Qty, BudgetCode, GLAccount, Remark
                FROM         MaterialDetail
                WHERE     (DTB = @DTB) AND (RequestNo = @RequestNo) ", CommandType.Text);
            AddParameter(MaterialDetailInfo.Field.DTB.ToString(), DTB);
            AddParameter(MaterialDetailInfo.Field.RequestNo.ToString(), reqNo);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetByPos(
        String DTB,
        int pos, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetByIndexName);
            AddParameter("INX", pos);
            AddParameter(MaterialDetailInfo.Field.DTB.ToString(), DTB);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public int GetCountRecord(
        String DTB,
        ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitSPCommand(_strSPGetCountName);
            AddParameter(MaterialDetailInfo.Field.DTB.ToString(), DTB);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(MaterialDetailInfo objEntr, ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitCommand(@"INSERT INTO MaterialDetail
                      (DTB, RequestNo, DetailNo, ItemCode, Description, ModelPN, Unit, Qty, BudgetCode, GLAccount, Remark)
                VALUES     (@DTB,@RequestNo,@DetailNo,@ItemCode,@Description,@ModelPN,@Unit,@Qty,@BudgetCode,@GLAccount,@Remark)", CommandType.Text);
            AddParameter(MaterialDetailInfo.Field.DTB.ToString(), objEntr.DTB);
            AddParameter(MaterialDetailInfo.Field.RequestNo.ToString(), objEntr.RequestNo);
            AddParameter(MaterialDetailInfo.Field.DetailNo.ToString(), objEntr.DetailNo);
            AddParameter(MaterialDetailInfo.Field.ItemCode.ToString(), objEntr.ItemCode);
            AddParameter(MaterialDetailInfo.Field.Description.ToString(), objEntr.Description);
            AddParameter(MaterialDetailInfo.Field.ModelPN.ToString(), objEntr.ModelPN);
            AddParameter(MaterialDetailInfo.Field.Unit.ToString(), objEntr.Unit);
            AddParameter(MaterialDetailInfo.Field.Qty.ToString(), objEntr.Qty);
            AddParameter(MaterialDetailInfo.Field.BudgetCode.ToString(), objEntr.BudgetCode);
            AddParameter(MaterialDetailInfo.Field.GLAccount.ToString(), objEntr.GLAccount);
            AddParameter(MaterialDetailInfo.Field.Remark.ToString(), objEntr.Remark);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(MaterialDetailInfo objEntr)
        {
            openConnection();
            InitCommand(@"UPDATE    MaterialDetail
                    SET              ItemCode = @ItemCode, [Description] = @Description, ModelPN = @ModelPN, Unit = @Unit, Qty = @Qty, BudgetCode = @BudgetCode, GLAccount = @GLAccount, 
                                      Remark = @Remark
                WHERE     (DTB = @DTB) AND (RequestNo = @RequestNo) AND (DetailNo = @DetailNo)", CommandType.Text);
            AddParameter(MaterialDetailInfo.Field.DTB.ToString(), objEntr.DTB);
            AddParameter(MaterialDetailInfo.Field.RequestNo.ToString(), objEntr.RequestNo);
            AddParameter(MaterialDetailInfo.Field.DetailNo.ToString(), objEntr.DetailNo);
            AddParameter(MaterialDetailInfo.Field.ItemCode.ToString(), objEntr.ItemCode);
            AddParameter(MaterialDetailInfo.Field.Description.ToString(), objEntr.Description);
            AddParameter(MaterialDetailInfo.Field.ModelPN.ToString(), objEntr.ModelPN);
            AddParameter(MaterialDetailInfo.Field.Unit.ToString(), objEntr.Unit);
            AddParameter(MaterialDetailInfo.Field.Qty.ToString(), objEntr.Qty);
            AddParameter(MaterialDetailInfo.Field.BudgetCode.ToString(), objEntr.BudgetCode);
            AddParameter(MaterialDetailInfo.Field.GLAccount.ToString(), objEntr.GLAccount);
            AddParameter(MaterialDetailInfo.Field.Remark.ToString(), objEntr.Remark);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        String DTB,
        String RequestNo,
        Int32 DetailNo = -1
        )
        {
            openConnection();
            string sql = @"DELETE FROM MaterialDetail
                WHERE     (DTB = @DTB) AND (RequestNo = @RequestNo)";
            if (DetailNo != -1) sql += " AND (DetailNo = @DetailNo)";
            InitCommand(sql, CommandType.Text);
            AddParameter(MaterialDetailInfo.Field.DTB.ToString(), DTB);
            AddParameter(MaterialDetailInfo.Field.RequestNo.ToString(), RequestNo);
            if (DetailNo != -1) AddParameter(MaterialDetailInfo.Field.DetailNo.ToString(), DetailNo);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        

        public Boolean IsExist(
        String DTB,
        String RequestNo,
        Int32 DetailNo = -1
        )
        {
            InitConnect();
            string sql = @"SELECT TOP(1) RequestNo FROM MaterialDetail
                WHERE     (DTB = @DTB) AND (RequestNo = @RequestNo)";
            if (DetailNo != -1) sql += " AND (DetailNo = @DetailNo)";
            InitCommand(sql, CommandType.Text);
            AddParameter(MaterialDetailInfo.Field.DTB.ToString(), DTB);
            AddParameter(MaterialDetailInfo.Field.RequestNo.ToString(), RequestNo);
            if (DetailNo != -1) AddParameter(MaterialDetailInfo.Field.DetailNo.ToString(), DetailNo);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(MaterialDetailInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            InitConnect();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }

            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetTransferOutName);
            AddParameter("DB", dtb);
            AddParameter("FROM", from);
            AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method

    }
}
